#include <math.h>

#include "user.h"

#include "user_doe.h"



double sign(double b);

double RK1(double u,double r1);

double RK2(double u,double v);

double RK3(double u,double r2,double r1);

extern double FI(double u,double r1);

static double h,w,k,alfa=M_PI_4,angle,Cos;

static double foc;

static double R3,R2,R1,D;



double letr(double u, double v) {

if(((u*u+v*v)>=R1*R1)&&((u*u+v*v)<R2*R2)){

  return (RK2(-v,u)-D/6*v)*k/foc;

 };

if(((u*u+v*v)>=R2*R2)&&((u*u+v*v)<R3*R3)){

  return (RK3(u*cos(alfa)+v*sin(alfa),R3,R2)+D/3*v+D/6*u)*k/foc;

 };

 if((u*u+v*v)<R1*R1) return (RK1(v,R1))/foc*k;



//return letr(-0.015,(R2-.015));

return -1000000.;

}

double letr1(double u, double v) {

return letr(Cos*u,v);

}

void letterR()   {

   char *InputValues[5];

   char *Title={" Focusator at letter R "};

   char *ParNames[]={

		     "Hight              :",

		     "Width              :",

		     "Discretization Step:",

		     "Angle              :",

		     "Focal      Distance:",

		     "WaveLength (mikron):",

		     "Focal Vertical Size:"

		    };



   char *Default[]={

		    "    12.8         ",

		    "    12.8         ",

		    "     .1          ",

		    "    0.0          ",

		    "   500.0         ",

		    "    1.06         ",

		    "    2.0          "

		   };

   if(!sedit(25,5,7,ParNames,InputValues,Default,Title))  return ;



   // read input data

   double s,wl;

   double a,b;

   sscanf(InputValues[0],"%lf \0",&h);

   sscanf(InputValues[1],"%lf \0",&w);

   sscanf(InputValues[2],"%lf \0",&s);

   sscanf(InputValues[3],"%lf \0",&angle);

   Cos=cos(angle*M_PI/180);

   sscanf(InputValues[4],"%lf \0",&a);

   sscanf(InputValues[5],"%lf \0",&wl);

   wl=wl/1000;

   k=2*M_PI/wl;

   sscanf(InputValues[6],"%lf \0",&b);

   foc=a;

   D=b;

   R3=h/2;

   R2=R3*sqrt((M_PI+3)/(3+M_PI+3));

   R1=R3*sqrt(3/(3+M_PI+5));



   // max&min values of phase in this mask

   double max=letr(0.,R1-.015);

   double min=letr(-.015,(R2-.015));

//   double max=B2,min=A2;

   NonSymmetricDOE(Phase, max, min, letr1);

   UndoMenu(2) ;   //  delete 2 last level submenu on the screen

   // control the DOE sizes and output they to InfoWindow

   // if sizes wrong (very big) - return

   if(MessageSize(w/Cos, h, s)) return;

   // Output information to InfoWindow

   MessageName("Focus-or at letter R ");  // write title

   MessageParam(1,"Focusator at the letter R");

   MessageParam(2,"Hight  %f",h);

   MessageParam(3,"Width  %f",w);

   MessageParam(4,"Step   %f",s);

   MessageParam(5,"Angle  %f",angle);

   MessageParam(6,"   A = %f",a);

   MessageParam(7,"   B = %f",b);

   MessageOut();                  // output all string to InfoWindow

   return;

}

double sign(double b)

{ if(b>0)return  1.;

  if(b<0)return -1.;

  return 0.;

};

double RK2(double u,double v)

{

  double C2=D/3;

  return sign(v)*C2*sqrt(u*u+v*v);

};



double RK1(double u,double r1)

{ double C=D/M_PI/r1/r1;

  return C*(r1*r1*(u*asin(u/r1)+sqrt(r1*r1-u*u)+r1*M_PI_2)-

	    pow((r1*r1-u*u),3/2)/3);

}

double RK3(double u,double r2,double r1)

{ double C2=D*sqrt(2)/3/4/M_PI_2/(r2*r2-r1*r1);

  double D2=D*sqrt(2)/3/4*r1*r1/(r2*r2-r1*r1);

  return C2*(

    r2*r2*( u*asin(u/r2)-M_PI_2*r2+sqrt(r2*r2-u*u) )-pow((r2*r2-u*u),3/2)/3

    -FI(u,r1)

	    )

	    +D2*(u+r2);

};



